package com.jss.lanouhn.fristapp.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.HomeTitlesInfo;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lanouhn on 16/8/31.
 */
public class HomeTitleAdapter extends RecyclerView.Adapter<HomeTitleAdapter.TitleViewHolder>{
    private Context mContext;
    private HomeTitlesInfo titlesInfos;

    public HomeTitleAdapter(Context mContext, HomeTitlesInfo titlesInfos) {
        this.mContext = mContext;
        this.titlesInfos = titlesInfos;
    }

    @Override
    public TitleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_title_rv,null);
        TitleViewHolder holder=new TitleViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(TitleViewHolder holder, int position) {
        HomeTitlesInfo.DataBean.PostsBean bean=titlesInfos.getData().getPosts().get(position);
        Picasso.with(mContext).load(bean.getCover_image_url()).into(holder.iv);

        holder.tv_title.setText(bean.getTitle());
        holder.tv_author.setText(bean.getAuthor().getNickname());
        holder.tv_times.setText(new SimpleDateFormat("MM-dd").format(new Date(bean.getCreated_at()*1000L))+"");
        holder.tv_likes.setText(bean.getLikes_count()+"");

    }

    @Override
    public int getItemCount() {

        return titlesInfos.getData().getPosts().size();
    }

    public class TitleViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView tv_title,tv_times,tv_author,tv_likes;

        public TitleViewHolder(View itemView) {
            super(itemView);
            iv= (ImageView) itemView.findViewById(R.id.iv_item_title);
            tv_title= (TextView) itemView.findViewById(R.id.tv_item_details);
            tv_times= (TextView) itemView.findViewById(R.id.tv_item_times);
            tv_author= (TextView) itemView.findViewById(R.id.tv_author);
            tv_likes= (TextView) itemView.findViewById(R.id.tv_item_likes);

        }
    }

}
